import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["side", "children"],
    _excluded2 = ["onOk", "onCancel"];

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(keyGen);

import React, { Component } from 'react'; // import { render } from 'react-dom';

import { Modal } from 'antd';
import { isPromise } from "../../utils/utils";

function keyGen(id) {
  return _regeneratorRuntime.wrap(function keyGen$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          if (!true) {
            _context.next = 5;
            break;
          }

          _context.next = 3;
          return "modal-".concat(id++);

        case 3:
          _context.next = 0;
          break;

        case 5:
        case "end":
          return _context.stop();
      }
    }
  }, _marked);
}

var KeyGen = keyGen(1); // let modalContainer;

var containerInstanse;
export function registerContainer(container) {
  containerInstanse = container;
}

var ModalContainer = /*#__PURE__*/function (_Component) {
  _inherits(ModalContainer, _Component);

  var _super = _createSuper(ModalContainer);

  function ModalContainer() {
    var _this;

    _classCallCheck(this, ModalContainer);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      modals: []
    };
    return _this;
  }

  _createClass(ModalContainer, [{
    key: "open",
    value: function open(modal) {
      var modals = this.state.modals;
      var props = modal; // props.key = KeyGen.next().value;

      props.visible = true;
      modals.push(props);
      this.setState({
        modals: modals
      });
    }
  }, {
    key: "confirmLoading",
    value: function confirmLoading(key) {
      this.setModalProp(key, {
        confirmLoading: true
      });
    }
  }, {
    key: "isLoading",
    value: function isLoading(key) {
      var modals = this.state.modals;
      var result = modals.filter(function (item) {
        return item.key === key;
      });
      var modal = result[0];
      return modal && modal.confirmLoading === true;
    }
  }, {
    key: "setModalProp",
    value: function setModalProp(key, props) {
      var modals = this.state.modals;
      var result = modals.filter(function (item) {
        return item.key === key;
      });
      var modal = result[0];

      if (modal) {
        var prop = _objectSpread(_objectSpread({}, modal), props);

        modals.splice(modals.indexOf(modal), 1, prop);
        this.setState({
          modals: modals
        });
      }
    }
  }, {
    key: "close",
    value: function close(key) {
      this.setModalProp(key, {
        visible: false
      });
    }
  }, {
    key: "handleAfterClose",
    value: function handleAfterClose(p) {
      var items = this.state.modals.filter(function (modal) {
        return modal.key !== p.key;
      });
      this.setState({
        modals: items
      });

      if (p.afterClose) {
        p.afterClose();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var modals = this.state.modals; // let maskKey;
      // modals.forEach((m,i)=>{
      //   if(i === modals.length - 1 || m.visible){
      //     maskKey = m.key;
      //   }
      // })

      var items = modals.map(function (props, index) {
        var side = props.side,
            children = props.children,
            other = _objectWithoutProperties(props, _excluded);

        var otherProps = other;

        if (side) {
          otherProps = _objectSpread({
            wrapClassName: "ant-modal-sidebar-".concat(side),
            transitionName: "move-".concat(side)
          }, other);
        }

        return (
          /*#__PURE__*/
          // maskTransitionName={false}
          // maskAnimation={false}
          React.createElement(Modal, Object.assign({
            key: props.key || index
          }, otherProps, {
            afterClose: _this2.handleAfterClose.bind(_this2, props),
            mask: true,
            destroyOnClose: true
          }), children)
        );
      });
      return /*#__PURE__*/React.createElement(React.Fragment, null, items);
    }
  }]);

  return ModalContainer;
}(Component);

export { ModalContainer as default };
export function getContainer() {
  // if (!modalContainer || !modalContainer.offsetParent) {
  //   const doc = window.document;
  //   modalContainer = doc.getElementById('modal-container');
  //   // const root = doc.getElementById('root');
  //   // modalContainer = doc.createElement('div');
  //   // modalContainer.id = 'modal-container';
  //   // root.appendChild(modalContainer);
  //   containerInstanse = render(<ModalContainer />, modalContainer);
  // }
  return containerInstanse;
}

var noop = function noop() {};

export function open(prop) {
  var props = prop;
  var container = getContainer();
  var key = KeyGen.next().value;

  var _prop$onOk = prop.onOk,
      onOk = _prop$onOk === void 0 ? noop : _prop$onOk,
      _prop$onCancel = prop.onCancel,
      onCancel = _prop$onCancel === void 0 ? noop : _prop$onCancel,
      otherProps = _objectWithoutProperties(prop, _excluded2);

  function close() {
    container.close(key);
  }

  var okFn = function okFn(fn) {
    return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var v;
      return _regeneratorRuntime.wrap(function _callee$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              v = fn();

              if (!isPromise(v)) {
                _context2.next = 14;
                break;
              }

              _context2.prev = 2;
              container.setModalProp(key, {
                confirmLoading: true
              });
              _context2.next = 6;
              return v;

            case 6:
              _context2.t0 = _context2.sent;

              if (!(_context2.t0 !== false)) {
                _context2.next = 9;
                break;
              }

              close();

            case 9:
              _context2.prev = 9;
              container.setModalProp(key, {
                confirmLoading: false
              });
              return _context2.finish(9);

            case 12:
              _context2.next = 15;
              break;

            case 14:
              if (v !== false) {
                close();
              }

            case 15:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee, null, [[2,, 9, 12]]);
    }));
  };

  var cancelFn = function cancelFn(fn) {
    return function () {
      if (fn() !== false && !container.isLoading(key)) {
        close();
      }
    };
  };

  props = _objectSpread(_objectSpread({
    key: key,
    onCancel: cancelFn(onCancel),
    onOk: okFn(onOk)
  }, otherProps), {}, {
    close: close
  });
  container.open(props);
  return {
    close: close,
    props: props
  };
}